<?php
class EventManager_Form_Element_Time extends Zend_Form_Element_Xhtml 
{
	protected $_timeFormat = '%hours%:%minutes%:%seconds%';
	protected $_hours ;
	protected $_minutes;
	protected $_seconds;
	
	public function __construct($spec,$options =null)
	{
		$this->addPrefixPath(
			'EventManager_Form_Decorator',
			APPLICATION_PATH.'/modules/eventManager/forms/decorator',
			'decorator'		
		);
		parent::__construct($spec,$options);	
	}
 	
	/*
	public function init()
    {
        parent::init();
        $this->addDecorator('ViewScript', array(
            'viewScript' =>  'time.phtml'
        ));
 
        $this->addValidator(new Zend_Validate_Regex('/^[0-9]+:[0-9]+:[0-9]+$/'));
    }
    */
	
	public function setHours($value)
	{
		$this->_hours = (int)$value;
		return $this;
	}
	
	public function getHours()
	{
		return $this->_hours;		
	}
	
	public function setMinutes($value)
	{
		$this->_minutes = (int)$value;
		return $this;
	}
	
	public function getMinutes()
	{
		return $this->_minutes;
	}
	
	public function setSeconds($value)
	{
		$this->_seconds	 = (int)$vlaue;
		return $this;
	}
	
	public function getSeconds()
	{
		return $this->_seconds;
	}
	
	public function getValue(){
		return str_replace(
			array('%hours%','%minutes%','%seconds%'),
			array($this->getHours(),$this->getMinutes(),$this->getSeconds()),
			$this->_timeFormat
		);
	}   
 
    public function setValue($value)
    {
    	
        if(is_string($value)){
        	$value = strtotime($value);
        }elseif(is_array($value)
        	&& isset($value['hour'])
        	&& isset($value['minute'])
        	&& isset($value['second'])  
        	){

        		$this ->setHours($value['hour'])
        		      ->setMinutes($value['minute'])
        		      ->setSeconds($value['second']);	
        }else{
        	throw new exception('Ongeldige waarden');
        }
        return $this;	
        
    }
    
    public function loadDefaultDecorators()
    {
    	if($this->loadDefaultDecoratorsIsDisabled()){
    		return;
    	}
    	
    	$decorators = $this->getDecorators();
    	if(empty($decorators)){
    		$this->addDecorator('Time')
    	         ->addDecorator('Errors')
    	         ->addDecorator('Description',array(
    	         	'tag'=> 'p',
    	         	'class'=>'description')
    	         )
    	         ->addDecorator('HtmlTag',array(
    	         	'tag'=>'dd',
    	         	'id'=>$this->getName().'-element')
    	         )
    	         ->addDecorator('Label',array('tag'=>'dt'));
    	}
    	
    }
}